import { ref, onMounted, onUnmounted } from "vue";
import * as echarts from "echarts";


export default function useEcharts(chartId, options) {
  const chart = ref(null);
  onMounted(() => {
    //初始化图表
    chart.value =echarts.init(document.getElementById(chartId));
    //设置图表参数
    chart.value.setOption(options);
    //窗口大小改变时，重新设置图表大小
    window.addEventListener("resize", () => {
      chart.value.resize();
    });
  });
 
  //销毁图表
  onUnmounted(() => {
    chart.value.dispose();
     // 移除窗口大小变化的监听器
    window.removeEventListener("resize", () => chart.value.resize());
  });
  

   // 更新 ECharts 配置项的方法
  const updateChart = (newOptions) => {
    chart.value.setOption(newOptions);
  };
  
   return { chart, updateChart };
}